package com.rd.student.jiaji.class1.jinhongyu.day0801;

import java.util.Arrays;

//存在一个数组，数组中的元素为 {1, 3, 5, 7, 9, 11, 13, 15, 17, 0}，
// 0是无效元素，仅占位使用，当前数组中有效元素个数为9，在该数组中元素9后面放入10，
// 其他元素依次后移，最终结果应为 {1, 3, 5, 7, 9, 10, 11, 13, 15, 17}
public class Shuzu {
    public static void main(String[] args) {


//        int[] arr = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
//        int index = 0;
//        for (int i = 0; i < arr.length; i++) {
//            if (arr[i]==9){
//                index = i+1;
//            }
//        }
//        for (int i = arr.length-1 ; i > index ; i--) {//从最后开始
//                arr[i]=arr[i-1];
//        }
//        arr[index] = 10;
//        ergodic(arr);

//        int[] arr = new int[]{1, 3, 5, 7, 9, 10, 11, 13, 15, 17};
//        int index = 0;
//        for (int i = 0; i < arr.length; i++) {
//            if (arr[i]==11) {
//                index = i;
//            }
//        }
//        for (int i = index; i < arr.length; i++) {
//            if (i!= arr.length-1) {
//                arr[i] = arr[i + 1];
//            }
//        }
//        arr[arr.length-1] = 0;
//
//        ergodic(arr);


        int[] arr1 = {1, 3, 5, 7, 9};
        int[] arr2 = {8, 6, 4, 2, 0};
//        arr1(50);
//        arr2(arr1,arr2);
//        transform(arr1);
//        System.out.println(arr5(arr1));
//        System.out.println(arr5(arr2));

    }

    public static void arr1(int i) {
        int[] arr = new int[i];
        for (int j = 0; j < i; j++) {
            arr[j] = j + 1;
        }

        ergodic(arr);

        int sum = summation(arr);
        System.out.println(sum);

        int aver = average(arr);
        System.out.println(aver);

        int max = arr_max(arr);
        System.out.println(max);

        int min = arr_min(arr);
        System.out.println(min);
    }//1

    public static void ergodic(int[] arr) {
        System.out.print("遍历:");
        for (int i1 : arr) {
            System.out.print(i1 + " ");
        }
        System.out.println();
    }

    public static int summation(int[] arr) {
        System.out.print("总和:");
        int a = 0;
        for (int i = 0; i < arr.length; i++) {
            a += arr[i];
        }
        return a;
    }

    public static int average(int[] arr) {
        System.out.print("平均值:");
        int a = 0;
        for (int i = 0; i < arr.length; i++) {
            a += arr[i];
        }
        return a / arr.length;
    }//3

    public static int arr_max(int[] arr) {
        System.out.print("最大值:");
        int a = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (a < arr[i]) {
                a = arr[i];
            }
        }
        return a;
    }

    public static int arr_min(int[] arr) {
        System.out.print("最小值:");
        int a = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (a > arr[i]) {
                a = arr[i];
            }
        }
        return a;
    }

    public static void arr2(int[] arr1, int[] arr2) {
        int[] arr = new int[arr1.length + arr2.length];
        System.arraycopy(arr1, 0, arr, 0, arr1.length);
        System.arraycopy(arr2, 0, arr, arr1.length, arr2.length);
        Arrays.sort(arr);
        ergodic(arr);
    }//2

    public static void transform(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
        ergodic(arr);
    }//4

    public static boolean arr5(int[] arr) {
        boolean flag = true;
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                flag = false;
            }
        }
        return flag;
    }//5

    public static int[] copyOf(int[] arr) {
        int[] arr1 = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
        }
        return arr1;
    }//6

    public static void toString(int[] arr) {
        System.out.print("["+arr[0]);
        for (int i = 1; i < arr.length; i++) {
            System.out.print(","+arr[i]);
        }
        System.out.println("]");
    }//7


}
